package com.guangzhi.weijianzhi.activity;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.guangzhi.weijianzhi.R;
import com.guangzhi.weijianzhi.http.HttpRequestUtils;
import com.guangzhi.weijianzhi.http.MyAsyncHttpResponseHandler;
import com.guangzhi.weijianzhi.view.LodingFragmentDialog;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.umeng.analytics.MobclickAgent;

/**
 * @author 注册
 */
public class RegisterActivity extends BaseActivity implements
		OnClickListener {

	private EditText phoneEdt, passwordEdt, okPasswordEdt, verificationEdt;

	private Button verificationBtn, registBtn;
	private TimeCount time; // 倒计时内部类


	private CheckBox cb;
	private TextView mTv_text;

	private EditText mPhone;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.registratactivity);
		
		initView();
		//initView();
		//onclicklistener();
	}

	/*public void initView() {
		initTitle();
		titleTextV.setText("注册");
		phoneEdt = (EditText) this.findViewById(R.id.phone_edt);
		passwordEdt = (EditText) this.findViewById(R.id.password_edt);
		okPasswordEdt = (EditText) this.findViewById(R.id.ok_password_edt);
		verificationEdt = (EditText) this.findViewById(R.id.yanzheng_edt);
		verificationBtn = (Button) this.findViewById(R.id.get_yanzheng_btn);
		registBtn = (Button) this.findViewById(R.id.regist_btn);
		time = new TimeCount(60000, 1000);
		registBtn.setText("注\t\t册");
		cb = (CheckBox) findViewById(R.id.checkBox);
		mTv_text = (TextView) findViewById(R.id.tv_text);
		mTv_text.setOnClickListener(this);
	}*/


//	public void onclicklistener() {
//		goBack.setOnClickListener(this);
//		verificationBtn.setOnClickListener(this);
//		registBtn.setOnClickListener(this);
//	}

	private void initView() {
		
		initTitle();
		
	    titleTextV.setText("注册");
	    mPhone = (EditText) findViewById(R.id.phone_edt);
	    findViewById(R.id.regist_btn).setOnClickListener(this);
	    goBack.setOnClickListener(this);
		
		
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		
		case R.id.regist_btn:
			if(mPhone.getText().toString()==null || "".equals(mPhone.getText().toString())){
				toastUtil("请输入您要注册的手机号码");
			}else{
				if(mPhone.getText().toString().length()==11 && "1".equals(mPhone.getText().toString().substring(0, 1))){
					requestCode(mPhone.getText().toString());
				}else{
					toastUtil("请输入正确的手机号码");
				}
			}
			break;
		case R.id.title_back:
			onBackPressed();
			break;
		
		/*
		case R.id.get_yanzheng_btn:

			if (phoneEdt.getText().toString().equals("")) {
				Toast.makeText(RegisterActivity.this, "请输入手机号",
						Toast.LENGTH_SHORT).show();
			} else {//设置背景为灰色
				verificationBtn.setBackgroundResource(R.drawable.login_right_pre_bg);
				time.start();// 倒计时开始；获取验证码
				requestCode(phoneEdt.getText().toString());
			}
			break;

		case R.id.regist_btn:
			if (phoneEdt.getText().toString().equals("")) {
				toastUtil("请输入注册手机号");
			} else if (passwordEdt.getText().toString().equals("")) {
				toastUtil("请输入注册密码");
			} else if (okPasswordEdt.getText().toString().equals("")) {
				toastUtil("请确认密码");
			} else if (!passwordEdt.getText().toString()
					.equals(okPasswordEdt.getText().toString())) {
				toastUtil("两次输入密码不一致,请重新输入");
			} else if (verificationEdt.getText().toString().equals("")) {
				toastUtil("请输入验证码");
			} else if (!cb.isChecked()) {
				toastUtil("请同意用户协议");
			} else {
				//Todo  注册成功跳转   到时候注册成功，成功以后跳转
				
				doRegister(phoneEdt.getText().toString(),passwordEdt.getText().toString(),verificationEdt.getText().toString());
				
				Intent intent = new Intent(this,StatusSelectActivity.class);
				intent.putExtra("phone", phoneEdt.getText().toString());
				intent.putExtra("password", okPasswordEdt.getText().toString());
				startActivity(intent);
			}
			break;

		case R.id.tv_text:
			Intent i = new Intent(RegisterActivity.this, XieYiActivity.class);
			startActivity(i);
			break;
			
		case R.id.title_back:
			onBackPressed();
			break;
		*/}
	}
     private void doRegister(String tel, String password, String messageCode) {
		// TODO Auto-generated method stub
    	 HttpRequestUtils.doHttpRegister(tel, password, messageCode, new MyAsyncHttpResponseHandler(getSupportFragmentManager(),getResources()) {
    		 
    		 @Override
    		public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
    			// TODO Auto-generated method stub
    			super.onSuccess(arg0, arg1, arg2);
    			
    			try {
					JSONObject jsonO = new JSONObject(new String(arg2));
					
					
					
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
    			
    		}
    		 
    		 @Override
    		public void onFinish() {
    			// TODO Auto-generated method stub
    			super.onFinish();
    			LodingFragmentDialog.dismiss(getSupportFragmentManager());
    		}
    		 
		});
	}

	private void requestCode(String tel) {
           LodingFragmentDialog.showLodingDialog(getSupportFragmentManager(), getResources());
			HttpRequestUtils.doHttpRegisterCode(tel, new AsyncHttpResponseHandler() {

				@Override
				public void onFailure(int arg0, Header[] arg1, byte[] arg2,
						Throwable arg3) {
					// TODO Auto-generated method stub
				toastUtil("网络不给力");
					
				}

				@Override
				public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
					// TODO Auto-generated method stub
					try {
						JSONObject jsonO = new JSONObject(new String(arg2));
						
						if(!jsonO.optBoolean("status")){//{"data":{"code":872890},"errMessage":"","status":true}
							toastUtil(jsonO.optString("errMessage"));
						}else{
							JSONObject optJSONObject = jsonO.optJSONObject("data");
							
							String code = optJSONObject.optString("code");
							
							Intent intent = new Intent(RegisterActivity.this, RequestCodeActivity.class);
							intent.putExtra("phone",mPhone.getText().toString());
							intent.putExtra("code", code);
							startActivity(intent);
							
							
						}
						
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
				}
				@Override
				public void onFinish() {
					// TODO Auto-generated method stub
					super.onFinish();
					LodingFragmentDialog.dismiss(getSupportFragmentManager());
				}
				
			});
		
	}

	

	public void toastUtil(String str) {
		Toast.makeText(RegisterActivity.this, str, Toast.LENGTH_SHORT).show();
	}


	/**
	 * @author Administrator 自定义一个倒计时的内部类；
	 */
	class TimeCount extends CountDownTimer {
		public TimeCount(long millisInFuture, long countDownInterval) {
			super(millisInFuture, countDownInterval);// 参数依次为总时长,和计时的时间间隔
		}

		// 当时间到的时候；
		public void onFinish() {
			// TODO Auto-generated method stub
			verificationBtn.setText("获取验证码");
			verificationBtn.setClickable(true);
			verificationBtn.setBackgroundResource(R.drawable.login_right_bg);
		}

		@Override
		public void onTick(long millisUntilFinished) {
			verificationBtn.setClickable(false);
			verificationBtn.setText(millisUntilFinished / 1000 + "秒");
		}
	}

}
